import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:pwd_keeper/global.dart';
import 'package:pwd_keeper/iconfont.dart';
import 'package:pwd_keeper/pages/pwd/list.dart';
import 'package:pwd_keeper/pages/pwd/search.dart';
import 'package:pwd_keeper/router/application.dart';

class PwdKeeperIndexPage extends StatefulWidget {
  PwdKeeperIndexPage({Key key}) : super(key: key);
  @override
  _PwdKeeperIndexPageState createState() => _PwdKeeperIndexPageState();
}

class _PwdKeeperIndexPageState extends State<PwdKeeperIndexPage> {
  String searchText = '';

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        actions: <Widget>[
          IconButton(
            icon: Icon(IconFont.plus),
            tooltip: '添加新密码',
            onPressed: () {
              var keeper = Uri.encodeComponent(json.encode({
                'name': '',
                'account': '',
                'password': '',
                'desc': '',
                'link': '',
                'icon': 'default',
              }));
              Application.router.navigateTo(
                context,
                '/keeper_detail_page?keeper=$keeper',
              );
            },
          ),
        ],
        title: Center(child: Icon(IconFont.mima)),
      ),
      body: Container(
        padding: EdgeInsets.fromLTRB(25, 5, 25, 0),
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(color: Global.backgroundColor),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            PwdKeeperSearchPage(onChanged: (text) {
              setState(() {
                searchText = text;
              });
            }),
            SizedBox(height: 15),
            Expanded(
              child: PwdKeeperListPage(searchText: searchText),
            ),
          ],
        ),
      ),
    );
  }
}
